package com.msmall.base.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import com.msmall.community.common.Configuration;

/**
 * 水印
 */
public class WatermarkerUtil {
	
	public static final String TEXT = Configuration.getValue(Configuration.WATERMARKER_TEXT);
	public static final String URL = Configuration.getValue(Configuration.WATERMARKER_URL);
	public static final String IMAGE = Configuration.getValue(Configuration.WATERMARKER_IMAGE);
	
	public static String DEFAULT_FONTNAME = "宋体";
	
	static{
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		String[] fontNames = ge.getAvailableFontFamilyNames();
		boolean found = false;
		for(String name : fontNames){
			if(name.equals(DEFAULT_FONTNAME)){
				found = true;
				break;
			}
		}
		if(!found){
			DEFAULT_FONTNAME = fontNames[0];
		}
	}
	/**
	 * 在左下角加
	 */
	public static void markText(File target, String fileExt, String fontName, int fontStyle, int fontSize){
		try{
			Image src = ImageIO.read(target);
			int width = src.getWidth(null);
			int height = src.getHeight(null);
			BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics2D g = image.createGraphics();
			g.drawImage(src, 0, 0, width, height, null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1f));
			g.setColor(Color.RED);
			if(StringUtil.nullOrBlank(fontName)){
				fontName = DEFAULT_FONTNAME;
			}
			if(fontStyle == 0){
				fontStyle = Font.BOLD;
			}
			if(fontSize == 0){
				fontSize = 20;
			}
			g.setFont(new Font(fontName, fontStyle, fontSize));
			g.drawString(TEXT, 10, height - fontSize*2);
			g.drawString(URL, 10, height - fontSize);
			g.dispose();
			String ext = "";
			if(!StringUtil.nullOrBlank(fileExt)){
				if(fileExt.indexOf("image") != -1){ // fileExt = imge/jpg
					ext = fileExt.substring(fileExt.indexOf("/") + 1);
				}
				if(fileExt.indexOf(".") != -1){
					ext = fileExt.substring(fileExt.indexOf(".") + 1);
				}
			}
			ImageIO.write((BufferedImage) image, ext, target); 
		} catch(Exception e) {
		}
	}
	/**
	 * 在左下角加
	 */
	public static void markImage(File target, String fileExt){
		try{
			Image src = ImageIO.read(target);
			int width = src.getWidth(null);
			int height = src.getHeight(null);
			BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics2D g = image.createGraphics();
			g.drawImage(src, 0, 0, width, height, null);
			//水印文件
			String markerPath = IMAGE.startsWith("/")?IMAGE.substring(1):IMAGE;
			markerPath = Constants.ROOTPATH + markerPath;
			File marker = new File(markerPath);
			Image markerImg = ImageIO.read(marker);
			int markerWidth=markerImg.getWidth(null);
			int markerHeight=markerImg.getHeight(null);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1f));
			g.drawImage(markerImg, 0, height - markerHeight, markerWidth, markerHeight, null);
			//水印文件结束
			g.dispose();
			String ext = "";
			if(!StringUtil.nullOrBlank(fileExt)){
				if(fileExt.indexOf("image") != -1){ // fileExt = imge/jpg
					ext = fileExt.substring(fileExt.indexOf("/") + 1);
				}
				if(fileExt.indexOf(".") != -1){
					ext = fileExt.substring(fileExt.indexOf(".") + 1);
				}
			}
			ImageIO.write((BufferedImage) image, ext, target); 
		}catch(Exception e){}
	}
}
